package light.head.controller.site;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.site.CommodityDAO;
import light.head.dao.site.CommodityShowDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.CommodityShow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 公司产品展示
 */
@Controller
@RequestMapping(Url.COMMODITYSHOW)
public class CommodityShowController extends SupporterController{
		private CommodityShowDAO csDAO;
		private CommodityDAO commDAO;
		
		@RequestMapping(value = Url.INIT)
		public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
			return new ModelAndView(Url.COMMODITYSHOW_INIT);
		}

		@RequestMapping(Url.QUERY)
		public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
			PageBean pb = createPageBean(request);
			Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.SORT, Parm.DELETED, Parm.OC);
			if (csDAO.findByMap(map, pb) == RESULT.SUCCESS){
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}		
			return new ModelAndView(Url.COMMODITYSHOW_LIST);
		}
		
		@RequestMapping(Url.EDIT)
		public ModelAndView edit(Integer code,Integer wgpc, HttpServletRequest request, HttpServletResponse response) throws Exception {
			if (null != code) {
				CommodityShow cs = csDAO.get(code);
				request.setAttribute(Parm.COMMODITYSHOW, cs);
			}
			return new ModelAndView(Url.COMMODITYSHOW_DEIT);
		}
		
		@RequestMapping(Url.BATCHEDIT)
		public ModelAndView batchEdit( HttpServletRequest request, HttpServletResponse response) throws Exception {
			return new ModelAndView(Url.BATCH_EDIT);
		}
		//单个添加
		@RequestMapping(value = Url.SAVE)
		public void save(CommodityShow cr,HttpServletRequest request, HttpServletResponse response) throws Exception {
			RESULT result = RESULT.ERROR;
			if (cr.getCode() == null) {
				cr.setDeleted(false);
				cr.setCu(cu_code(request));
				cr.setOc(cu_organCode(request));
				cr.setCt(new Date());
				result = csDAO.add(cr);
			} else {
				result = csDAO.upd(cr);
			}
			if (result == RESULT.SUCCESS) {
				print(response, Msg.SUCCESS);
			} else {
				print(response, Msg.ERROR);
			}
		}
		//批量添加
		@RequestMapping(value = Url.SAVEBATCH)
		public void saveBatch(CommodityShow cr,HttpServletRequest request, HttpServletResponse response) throws Exception {
			RESULT result = RESULT.ERROR;
			if(cr!=null){
				for(int i = 0; i<cr.getList().size();i++){
					if(cr.getList().get(i).getName()!=null){
						cr.getList().get(i).setDeleted(false);
						cr.getList().get(i).setCu(cu_code(request));
						cr.getList().get(i).setOc(cu_organCode(request));
						cr.getList().get(i).setCt(new Date());
						result = csDAO.add(cr.getList().get(i));
					}
				}
			}
			if (result == RESULT.SUCCESS) {
				print(response, Msg.SUCCESS);
			} else {
				print(response, Msg.ERROR);
			}
		}
				
		@RequestMapping(value = Url.DELETE)
		public void delete(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
			RESULT result = RESULT.ERROR;
			if(code!=null){
				CommodityShow cs = csDAO.get(code);
				cs.setDeleted(true);
				result=csDAO.upd(cs);
			}
			if(result==RESULT.SUCCESS){
				print(response, Msg.SUCCESS);
			}else{
				print(response, Msg.ERROR);
			}
		}
		
		//单个辅助查询
		@RequestMapping(Url.QUERYCOMMODITYINIT)
		public ModelAndView queryCommodityInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
			return new ModelAndView(Url.QUERYCOMMODITY_INIT);
		}
		@RequestMapping(value = Url.QUERYCOMMODITYLIST)
		public ModelAndView queryCommodityList(HttpServletRequest request, HttpServletResponse response) throws Exception {
			PageBean pb = createPageBean(request);
			Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.OC, Parm.DELETED);
			if (commDAO.findByMap(map, pb) == RESULT.SUCCESS) {
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			return new ModelAndView(Url.QUERYCOMMODITY_LIST);
		}
		//批量辅助查询
		@RequestMapping(Url.BATCHCOMMODITYINIT)
		public ModelAndView batchCommodityInit(Integer index, HttpServletRequest request, HttpServletResponse response) throws Exception {
			request.setAttribute("index", index);
			return new ModelAndView(Url.BATCHCOMMODITY_INIT);
		}
		@RequestMapping(value = Url.BATCHCOMMODITYLIST)
		public ModelAndView batchCommodityList(HttpServletRequest request, HttpServletResponse response) throws Exception {
			PageBean pb = createPageBean(request);
			Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.OC, Parm.DELETED);
			if (commDAO.findByMap(map, pb) == RESULT.SUCCESS) {
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			return new ModelAndView(Url.BATCHCOMMODITY_LIST);
		}
		
		@Autowired
		public void setCommDAO(CommodityDAO commDAO) {
			this.commDAO = commDAO;
		}
		
		@Autowired
		public void setCsDAO(CommodityShowDAO csDAO) {
			this.csDAO = csDAO;
		}
}
